import axios from 'axios';

const API_URL = 'http://localhost:9000'; // 替换为你的后端地址

export class ReportExportService{

    static async getAllAuditedReports(){
        try {
            const response = await axios.get(`${API_URL}/projectReport/getAllChecked`);
            return response.data;
          } catch (error) {
            console.error('获取报告列表失败:', error);
            throw error;
          }
    }

    static async exportReportToWord(id: any) {
        try {
            const response = await axios.get(`${API_URL}/export/word`, {
                params: { reportId: id }, // 传递 reportId 作为参数
                responseType: 'blob', // 确保以 blob 格式接收文件
            });
    
            // 创建一个 URL 并触发下载
            const url = window.URL.createObjectURL(new Blob([response.data]));
            const link = document.createElement('a');
            link.href = url;
            link.setAttribute('download', `Report_${id}.docx`); // 设置下载文件名
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
    
            console.log('报告导出成功');
        } catch (error) {
            console.error('报告导出失败:', error);
            throw error;
        }
    }
}
